package com.njlime.core.web;

/**
 * Created by Dean on 2017-7-20.
 */
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.njlime.sys.security.SystemAuthorizingRealm;
import com.njlime.sys.security.util.CacheUtils;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;

public class JcaptchaServlet extends HttpServlet {

    private static final long serialVersionUID = -8021621053846019170L;
    public static final String CAPTCHA_IMAGE_FORMAT = "jpeg";

    private ImageCaptchaService captchaService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

        captchaService = (ImageCaptchaService) BeanFactoryUtils.beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class);
    }

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        byte[] captchaChallengeAsJpeg = null;

        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
            // 如果已经登录，获取二维码时退出原账号
            SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
            if (principal != null && !principal.isMobileLogin()) {
                CacheUtils.remove(UserUtils.CACHE_AUTH_INFO_ + UserUtils.getUser().getId());
                UserUtils.getSubject().logout();
            }
            String captchaId = request.getSession().getId();
            BufferedImage challenge = captchaService.getImageChallengeForID(
                    captchaId, request.getLocale());

            ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
        } catch (IllegalArgumentException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (CaptchaServiceException e) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

        // flush it in the response
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT);

        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}